package com.chenhs.oop.study.多线程;

/**
 * @program: JAVA300
 * @description: 线程的优先级
 * 默认5  范围1-10
 * 数字越小优先级越高，
 * 必须在线程启动前设置。
 * @author: peanut
 * @create: 2019-07-21 14:12
 */
public class Demo5 implements Runnable {

    static int thread0 = 0;
    static int thread1 = 0;
    static int thread2 = 0;
    static int thread3 = 0;
    static int thread4 = 0;

    @Override
    public void run() {
        for (int i = 0; i < 500; i++) {
            /*这个统计无意义的不管执行的优先级是什么，都要执行完整个循环啊，是不是傻？？？嗯？？？？*/
            switch (Thread.currentThread().getName()) {
                case "线程0":
                    System.out.println(Thread.currentThread().getName() + "线程0" + "--" + i + "|" + thread0);
                    thread0 = thread0 + 1;
                    break;
                case "线程1":
                    thread1 = thread1 + 1;
                    break;
                case "线程2":
                    thread2 = thread2 + 1;
                    break;
                case "线程3":
                    thread3 = thread3 + 1;
                    break;
                case "线程4":
                    thread4 = thread4 + 1;
                    break;
            }
            System.out.println(Thread.currentThread().getName() + "：" + i);
            Thread.yield();
        }
        System.out.println("线程0:" + thread0 + ",线程1:" + thread1 + ",线程2:" + thread2 + ",线程3:" + thread3 + ",线程4:" + thread4);
    }

    public static void main(String[] args) {
        /*创建线程*/
        Thread thread = new Thread(new Demo5(), "线程0");
        Thread thread1 = new Thread(new Demo5(), "线程1");
        Thread thread2 = new Thread(new Demo5(), "线程2");
        Thread thread3 = new Thread(new Demo5(), "线程3");
        Thread thread4 = new Thread(new Demo5(), "线程4");
        /*设置优先级*/
        thread.setPriority(1);
        thread1.setPriority(3);
        thread2.setPriority(7);
        thread3.setPriority(1);
        thread4.setPriority(10);
        /*启动线程*/
        thread.start();
        thread1.start();
        thread2.start();
        thread3.start();
        thread4.start();
    }
}
